<?php 
	if ($_POST)
	{
		if (isset($_POST['form_en']))
		{
			if (isset($_POST['json_encode']) && $_POST['json_encode'])
			{
				$json_encodearr = explode("\r\n", $_POST['json_encode']);
				$json_encodestr = json_encode($json_encodearr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
			}
			else
			{
				$msg_en = "请输入你要转换的字符串";
			}
		}
		else
		{
			if (isset($_POST['json_decode']) && $_POST['json_decode'])
			{
				if ( ! preg_match("/[^\d-., ]/", $_POST['json_decode']) || is_null(json_decode($_POST['json_decode'])))
				{
					$msg_de = "输入的字符串不是合法的json格式！";
				}
				else
				{
					$json_decodearr = json_decode($_POST['json_decode'], true);
					$json_decodestr = implode("\r\n", $json_decodearr);
				}
			}
			else
			{
				$msg_de = "请输入你要转换的字符串";
			}
		}
	}
?>
<!DOCTYPE html> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSON-TRANSFER</title>
<link href="css/bootstrap.css" rel="stylesheet">
<script type="text/javascript" src="js/jq.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script>
$(document).ready(function(){
	if(location.hash) 
	{
		$('a[href=' + location.hash + ']').tab('show');
	}
	$(document.body).on("click", "a[data-toggle]", function(event){
		location.hash = this.getAttribute("href");
	});
});
$(window).on('popstate', function(){
	var anchor = location.hash || $("a[data-toggle=tab]").first().attr("href");
	$('a[href=' + anchor + ']').tab('show');
});
</script>
</head>
<body>
<ul style="width:67%; margin: auto;" id="myTab" class="nav nav-tabs">
   <li class="active"><a href="#encode" data-toggle="tab">字符串转换为Json</a></li>
   <li><a href="#decode" data-toggle="tab">Json转换为字符串</a></li>
</ul><br/><hr style="margin-top: -21px;">
<div style="width:67%;" class="container tab-content">
	<div class="tab-pane fade in active" id="encode">
		<form method="post">
			<fieldset>
				<legend>字符串转换为Json</legend>
				<label>说明：可填入多个记录，每条记录用回车分开</label>
				<textarea rows="15" name="json_encode" id="json_encode"><?=isset($_POST['json_encode']) ? $_POST['json_encode'] : '' ?></textarea><br />
				<input type="submit" name="form_en" class="btn" value="转换" />
			</fieldset>
		</form>
		<?if (isset($json_encodestr)):?>
			输出结果<br/>
			<input style="width: 500px;" type="text" name="result_encode" id="result_encode" value="<?=htmlspecialchars($json_encodestr)?>" />
		<?elseif (isset($msg_en)):?>
			<span style="color: red;"><?=$msg_en?></span>
		<?endif;?>
	</div>
	<div class="tab-pane fade" id="decode">
		<form method="post">
			<fieldset>
				<legend>Json转换为字符串</legend>
				<label>说明：请填入Json字符串</label>
				<textarea rows="15" name="json_decode" id="json_decode"><?=isset($_POST['json_decode']) ? $_POST['json_decode'] : '' ?></textarea>
				<?if (isset($msg_de)):?>
					<span id="msg_de" style="color: red;"><?=$msg_de?></span>
				<?else:?>
					<?if (isset($json_decodestr)):?>
						<span style="color: rgb(255, 140, 60); font-family:sans-serif; font-size:40px; font-weight: bolder; padding-top: 120px; position:relative; padding-left: 10px; padding-right: 10px;">→</span>
						<textarea rows="15"><?=$json_decodestr?></textarea>
					<?endif;?>
				<?endif;?>
				<br/>
				<input type="submit" name="form_de" class="btn" value="转换" />
			</fieldset>
		</form>
	</div>
</div>
</body>
</html>
